About 1031 letters

About 5 minutes

#getattr

Description: Retrieve the value of an attribute. See also hasattr, delattr, and setattr.

def getattr(obj, name: str): ''' Retrieve the value of an attribute. :param obj: An object :param name: The name of the attribute '''

Example:

class Person: def __init__(self, name, age): self.name = name self.age = age p = Person("Alice", 30) print(p.name) print(getattr(p, 'name'))

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

getattr(obj, name) is equivalent to obj.name. If the attribute does not exist, it raises an AttributeError. You can optionally provide a default value: getattr(obj, 'missing_attr', default_value).

Created in 6/9/2025

Updated in 6/9/2025